<template>
  <el-row type="flex" justify="space-between" align="middle" style="height: 40px; width:100%">
    <el-col>
      <!-- 左侧内容 -->
      <span>{{ treeNode.name }}</span>
    </el-col>
    <!-- span:跨度，span设置为4，说明这里有一列独占4份，一行默认是24份 -->
    <el-col :span="4">
      <el-row type="flex" justify="end">
        <!-- 两个内容 -->
        <el-col>{{ treeNode.manager }}</el-col>
        <el-col>
          <!-- 下拉菜单 -->
          <el-dropdown @command="operateDepartments">
            <!-- 这个sapn是下拉菜单默认显示的内容 -->
            <span>
              <!-- i得class使用的是element的icon图标 -->
              操作<i class="el-icon-arrow-down" />
            </span>
            <!-- dropdownmenu 是具体的下拉菜单选项，通过具名插槽dropdown来指定 -->
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="add">添加子部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" command="edit">编辑部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" command="delete">删除部门</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
import { delDepartmentByIdApi } from '@/api/departments'
export default {
  name: 'TreeTools',
  props: {
    treeNode: {
      type: Object,
      required: true
    },
    isRoot: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    operateDepartments(type) {
      if (type === 'add') {
      // 添加部门
      // 告诉父组件 显示弹层 treeNode是当前部门对象
        this.$emit('addDepts', this.treeNode)// 为何传出treeNode 因为是添加子部门需要当前部门的数据
      } else if (type === 'edit') {
      // 编辑部门
        this.$emit('editDepts', this.treeNode)
      } else {
      // 删除部门
        this.$confirm('确定要删除该部门吗', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          return delDepartmentByIdApi(this.treeNode.id)
        }).then(() => {
          this.$emit('updateDeparts')
          this.$message.success('删除成功')
        })
      }
    }
  }
}
</script>

<style>

</style>
